//
//  MJSegmentView.m
//  MirrorCharge
//
//  Created by 殷殷明静 on 16/10/21.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "MJSegmentView.h"

@interface MJSegmentView ()
@property (nonatomic, strong) UIButton *popButton;
@property (nonatomic, strong) UIButton *pushButton;
@property (nonatomic, copy) SelectedBlock block;
@end
@implementation MJSegmentView
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        // 圆角
        self.layer.cornerRadius = 10;
        self.layer.masksToBounds = YES;
        // 收回视图button
        _popButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _popButton.frame = CGRectMake(0, 0, self.bounds.size.width / 2, self.bounds.size.height);
        _popButton.backgroundColor = [UIColor redColor];
        // 设置图片
        [_popButton setImage:[UIImage imageNamed:@"65e57ffe040136132708085b102a21488351ed6682b0ef444c232c850433bcfe"] forState:0];
        [self addSubview:_popButton];
        // 弹出button
        _pushButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _pushButton.frame = CGRectMake(_popButton.bounds.size.width, 0, _popButton.bounds.size.width, _popButton.bounds.size.width);
        _pushButton.backgroundColor = [UIColor whiteColor];
        [_pushButton setImage:[UIImage imageNamed:@"65e57ffe040136132708085b102a21488351ed6682b0ef444c232c850433bcfe"] forState:0];
        [self addSubview:_pushButton];
        // 点击事件
        [_popButton addTarget:self action:@selector(popViewAction:) forControlEvents:UIControlEventTouchUpInside];
        [_pushButton addTarget:self action:@selector(pushViewAction:) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return self;
}
- (void)popViewAction:(UIButton *)btn {
    _index = 0;
    btn.backgroundColor = [UIColor redColor];
    _pushButton.backgroundColor = [UIColor whiteColor];
    [self segmentSelected];
}
- (void)pushViewAction:(UIButton *)btn {
    _index = 1;
    btn.backgroundColor = [UIColor redColor];
    _popButton.backgroundColor = [UIColor whiteColor];
    [self segmentSelected];
}
- (void)segmentSelected {
    self.block();
}
- (void)segmentAction:(SelectedBlock)block {
    _block = block;
}

@end
